package DP.prototype;

import java.util.ArrayList;

/**
 * @author msi
 * 浅拷贝
 */
public class ShallowCopy implements Cloneable{
    //定义一个私有变量
    private ArrayList<String> arrayList = new ArrayList<>();

    @Override
    protected Object clone(){
        ShallowCopy shallowCopy=null;
        try {
            shallowCopy= (ShallowCopy) super.clone();
            shallowCopy.arrayList = (ArrayList<String>) this.arrayList.clone();//加上这段代码就是深拷贝了
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return shallowCopy;
    }
    //设置arraylist的值
    public void setValue(String str){
        this.arrayList.add(str);
    }
    //获取arraylist的值
    public ArrayList<String> getValue(){
        return this.arrayList;
    }
}
